<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <article-item
        v-for="item in artList"
        :key="item.art_id"
        :obj="item"
        @click.native="$router.push(`/article_detail?aid=${item.art_id}`)"
        :showCross='false'
      ></article-item>
    </van-list>
  </div>
</template>

<script>
import ArticleItem from '@/components/ArticleItem.vue'
import { searchResultListAPI } from '@/api/search.js'
export default {
  name: 'SearchResult',
  data () {
    return {
      artList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  components: {
    ArticleItem
  },
  async created () {
    const res = await searchResultListAPI({
      q: this.$route.params.kw,
      page: this.page
    })
    this.artList = res.data.data.results
  },
  methods: {
    async onLoad () {
      if (this.artList.length > 0) {
        this.page++
        const res = await searchResultListAPI({
          q: this.$route.params.kw,
          page: this.page
        })
        if (res.data.data.results.length < 10) {
          this.finished = true
        }
        this.artList = this.artList.concat(res.data.data.results)
      }

      this.loading = false
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
